<?php
/**
 * 游戏-大转盘
 */
session_start();
$uid = @$_SESSION['id'] ? @$_SESSION['id'] : ''; //当前用户
if(!$uid){
	exit('请登录！');
}
//从game表中取值，查看游戏扣除积分
$xf = C_base::M('admin.gamegl')->game_s(array('id'=>1));
//从game_conf中取值
$tager = C_base::M('admin.gamegl')->gconf_ss(array('gameid'=>1),'jiangpin,jilv');


if($tager){
	$prize_arr =array();
	foreach ($tager as $k=>$v){
		$prize_arr[$k]['id'] = $k+1;
		$prize_arr[$k]['prize'] = $v['jiangpin'];
		$prize_arr[$k]['v'] = $v['jilv'];
		if($k==0){
			$prize_arr[$k]['min'] = 90 ;$prize_arr[$k]['max'] = 148 ;
		}elseif ($k==1){
			$prize_arr[$k]['min'] = 150 ;$prize_arr[$k]['max'] = 209 ;
		}elseif ($k==2){
			$prize_arr[$k]['min'] = 210 ;$prize_arr[$k]['max'] = 269 ;
		}elseif ($k==3){
			$prize_arr[$k]['min'] = 270 ;$prize_arr[$k]['max'] = 329 ;
		}elseif ($k==4){
			$prize_arr[$k]['min'] = -30 ;$prize_arr[$k]['max'] =29 ;
		}else{
			$prize_arr[$k]['min'] = 30 ;$prize_arr[$k]['max'] = 89 ;
		}
	}
} 

foreach ($prize_arr as $v) {
    $arr[$v['id']] = $v['v'];
}

$prize_id = getRand($arr); //根据概率获取奖项id 

$res = $prize_arr[$prize_id - 1]; //中奖项 
$min = $res['min'];
$max = $res['max'];
if ($res['id'] == 7) { //七等奖 
    $i = mt_rand(0, 5);
    $data['angle'] = mt_rand($min[$i], $max[$i]);
} else {
    $data['angle'] = mt_rand($min, $max); //随机生成一个角度 
}
$data['prize'] = $res['prize'];
echo json_encode($data);
if($data){
	$record = array('uid'=>$uid,'gamename'=>$xf['name'],'jifen'=>$xf['jifen'],'time'=>time());
	if($data['prize']){ //中奖
		$record['jiangpin'] = $res['prize'];
		$record['zt'] = 1;
		//向game_winning表中写入数据
		C_base::M('admin.gamegl')->winn_c(array('uid'=>$uid,'gameid'=>1,'phone'=>'111','jiangpin'=>$res['prize'],'time'=>time()));
	}
	//向game_record表中写入数据
	C_base::M('admin.gamegl')->record_c($record);
}

function getRand($proArr) {
    $data = '';
    $proSum = array_sum($proArr); //概率数组的总概率精度 
     
    foreach ($proArr as $k => $v) { //概率数组循环
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $v) {
            $data = $k;
            break;
        } else {
            $proSum -= $v;
        }
    }
    unset($proArr);

    return $data;
}
